package com.taomus.mytools.simplec;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import com.taomus.mytools.simplec.common.Label;
import com.taomus.mytools.simplec.common.SyntaxTuple;
import static com.taomus.mytools.simplec.common.SyntaxTuple.*;
import com.taomus.mytools.simplec.common.Token;
import com.taomus.mytools.simplec.common.Word;
import com.taomus.mytools.simplec.common.WordEnum;

public final class ScaningSytaxTuple {
	private final static ScaningSytaxTuple instance = new ScaningSytaxTuple();

	private ScaningSytaxTuple() {
	}

	public static ScaningSytaxTuple getInstance() {
		return instance;
	}
	/**
	 * 类型检测与类型自动转换 
	 * char > short > int > long >float > double
	 * int > double
	 * @param st
	 */
	private void _typeScan(SyntaxTuple st){
		WordEnum opcode = st.getOpCode();
		Token t1 = st.getToken(T1);
		Token t2 = st.getToken(T2);
		Token t3 = st.getToken(T3);
		switch(opcode){
		case MOV:
		default:
			break;
		}
	}
	private Token getIfSymbol(WordEnum op){
		switch(op){
		case LE:
			return Word.TK_GT;
		case LT:
			return Word.TK_GE;
		case GE:
			return Word.TK_LT;
		case GT:
			return Word.TK_LE;
		case NE:
			return Word.TK_EQ;
		case EQ:
			return Word.TK_NE;
		default:
			return Word.TK_ERROR;
		}
	}
	private void _if(LinkedList<SyntaxTuple> list){
		LinkedList<SyntaxTuple> newList = new LinkedList<>();
		SyntaxTuple tempSt = null;
		for(SyntaxTuple st : list){
			this._typeScan(st);
			//TODO
			if(st.getOpCode().getCode() <= WordEnum.LE.getCode() && st.getOpCode().getCode() >= WordEnum.EQ.getCode()){
				tempSt = st;
				newList.add(st);
			}else if(st.getOpCode() == WordEnum.IF){
				tempSt.setToken(OPCODE,this.getIfSymbol(tempSt.getOpCode()));
				st.setToken(T3,Label.create());
				newList.add(st);
			}else if(st.getOpCode() == WordEnum.IFELSE){
				st.setToken(T1, Label.pop());
				SyntaxTuple gotoSt = new SyntaxTuple();
				gotoSt.setToken(OPCODE, Word.KW_GOTO);
				gotoSt.setToken(T1,Label.create());
				newList.add(gotoSt);
				newList.add(st);
			}else if(st.getOpCode() == WordEnum.IFEND){
				st.t1(Label.pop());
				newList.add(st);
			}else{
				newList.add(st);
			}
		}
		list.clear();
		list.addAll(newList);
	}
	public LinkedList<SyntaxTuple> scaning(LinkedList<SyntaxTuple> tupleList) {
		this._if(tupleList);
		return tupleList;
	}
}
